const Controller = require("egg").Controller;


class JwtController extends Controller {
    async doLogin() {
        let user = this.ctx.request.body.user; //user.username;user.password
        if (user.username === "admin" && user.password === "123456") {
            let user_jwt = {
                username: user.username
            }
            let token = this.app.jwt.sign(user, this.app.config.jwt.secret)
            this.ctx.body = {
                code: 20000,
                token: token
            }
        } else {
            this.ctx.body = {
                code: 40000,
                msg: "用户名或密码错误"
            }
        }
    }
    async getMessage() {
        this.ctx.body = "hello jwt"
    }

    // async index() {
    //     // this.ctx.body = "hellojwt"
    //     let user = {
    //         username: "admin"
    //     }
    //     //egg-jwt
    //     // sign生成token 用户登录
    //     let token = this.app.jwt.sign(user, this.app.config.jwt.secret)
    //     // this.ctx.body = token

    //     //校验token
    //     try {
    //         let decode = this.app.jwt.verify(token, this.app.config.jwt.secret)
    //         this.ctx.body = decode
    //     } catch (e) {
    //         this.ctx.body = "token为能通过验证"
    //     }
    // }
}

module.exports = JwtController